/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 */

package com.nail.zhaogr.front.action;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Hashtable;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.web.util.HttpUtils;

import com.nail.zhaogr.model.Comments;
import com.nail.zhaogr.model.Members;
import com.nail.zhaogr.model.PublishInfo;
import com.nail.zhaogr.service.CommentsManager;
import com.nail.zhaogr.service.MembersManager;
import com.nail.zhaogr.service.PublishInfoManager;
import com.nail.zhaogr.utils.Constants;
import com.nail.zhaogr.vo.query.CommentsQuery;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */


public class CommentsAction extends BaseAction{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String QUERY_JSP = "/pages/Comments/query.jsp";
	protected static final String LIST_JSP= "/front/PublishInfo/commentsList.jsp";
	protected static final String CREATE_JSP = "/pages/Comments/create.jsp";
	protected static final String EDIT_JSP = "/pages/Comments/edit.jsp";
	protected static final String SHOW_JSP = "/pages/Comments/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/Comments/list.do";
	
	private CommentsManager commentsManager;
	
	private PublishInfoManager publishInfoManager;
	
	private MembersManager membersManager;
	
	private Comments comments;
	
	private PublishInfo publishInfo;
	
	private Long memberId =null;
	
	private Long myContactId =null;
	java.lang.Long id = null;
	private String[] items;
	private int flag;
	public void prepare() throws Exception {
		this.getPageUrl();
		if (isNullOrEmptyString(id)) {
			comments = new Comments();
		} else {
			comments = (Comments)commentsManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性 */
	public void setCommentsManager(CommentsManager manager) {
		this.commentsManager = manager;
	}	
	
	public Object getModel() {
		return comments;
	}
	
	public void setCommentId(java.lang.Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 进入查询页面 */
	public String query() {
		return QUERY_JSP;
	}
	
	/** 执行搜索 */
	public String list() {
		
		CommentsQuery query = newQuery(CommentsQuery.class,DEFAULT_SORT_COLUMNS);
		publishInfo = publishInfoManager.getById(query.getPublishInfoId());
		comments.setPublishInfo(publishInfo);
		Page page = commentsManager.findPage(query);
		savePage(page,query);
		this.setTitle("查看全部评价_"+publishInfo.getCategory().getCategoryName()+"-"+publishInfo.getCityThree()+"_"+publishInfo.getCityTwo()+"找工人"+"_");
		this.setMetaDesc(publishInfo.getProfile());
		return LIST_JSP;
	}
	
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	@SuppressWarnings("deprecation")
	public String save() {
		Members memberInfo = this.getMemberInfo();
		try {
			if(memberInfo==null)
			{
				//Flash.current().error("请先登录再发表评价");
				this.getResponse().getWriter().write("1");
				return  null;
			}
			if(memberInfo.getMemberId().equals(comments.getMemberId()))
			{
				//Flash.current().error("请先登录再发表评价");
				this.getResponse().getWriter().write("3");
				return  null;
			}
			if(!this.filterCommentsKeywords(comments)){
				//Flash.current().error("您所提交的信息中有不合法，不文明的词语，请修改后再发布");
				this.getResponse().getWriter().write("2");
				return null;
			}
			comments.setMemberId(memberInfo.getMemberId());
			comments.setCommentDate(new Timestamp(System.currentTimeMillis()));
			comments.setUpdateTime(new Timestamp(System.currentTimeMillis()));
			comments.setMemberId(this.getMemberInfo().getMemberId());
			String jsonData = commentsManager.saveComment(comments, memberInfo, flag,myContactId);
			this.getResponse().getWriter().write(jsonData);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String commentAttitude(){
		Members memberInfo = this.getMemberInfo();
		try {
		if(memberInfo==null)
		{
			//Flash.current().error("请先登录再发表评价");
			this.getResponse().getWriter().write("1");
			return  null;
		}
		if(memberInfo.getMemberId().equals(comments.getMemberId()))
		{
			//Flash.current().error("请先登录再发表评价");
			this.getResponse().getWriter().write("3");
			return  null;
		}
	
		comments.setCommentDate(new Timestamp(System.currentTimeMillis()));
		comments.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		comments.setMemberId(this.getMemberInfo().getMemberId());
		//处理服务态度评价
		String jsonData = commentsManager.saveAttitude(comments, memberInfo, flag);
		
		this.getRequest().getSession().setAttribute(Constants.MEMBERINFO_SESSION, memberInfo);
		this.getResponse().getWriter().write(jsonData);
		} catch (IOException e) {
		}
		return null;
	}
	
	public String check(){
		try {
			Comments temp = commentsManager.getComment(comments);
			if(temp!=null){
				//已经有过服务态度评价
				this.getResponse().getWriter().write("1");
			}else{
				this.getResponse().getWriter().write("0");
			}
		}
		catch (IOException e) {
		}
		return null;
	}
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public String update() {
		commentsManager.update(this.comments);
		return LIST_ACTION;
	}
	
	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			java.lang.Long id = new java.lang.Long((String)params.get("commentId"));
			commentsManager.removeById(id);
		}
		return LIST_ACTION;
	}

	public void setPublishInfoManager(PublishInfoManager publishInfoManager) {
		this.publishInfoManager = publishInfoManager;
	}

	public void setMembersManager(MembersManager membersManager) {
		this.membersManager = membersManager;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public PublishInfo getPublishInfo() {
		return publishInfo;
	}

	public void setPublishInfo(PublishInfo publishInfo) {
		this.publishInfo = publishInfo;
	}

	/**
	 * @return the myContactId
	 */
	public Long getMyContactId() {
		return myContactId;
	}

	/**
	 * @param myContactId the myContactId to set
	 */
	public void setMyContactId(Long myContactId) {
		this.myContactId = myContactId;
	}

}
